#include <caemuboardplanner/CAEmuBoardPlannerE.h>

// ***********************************************************************

// class EDROOMTopContext

// ***********************************************************************



	// CONSTRUCTORES***********************************************

CAEmuBoardPlanner::EDROOMTopContext::EDROOMTopContext (CAEmuBoardPlanner &act, bool_t & EDROOMpVarVSGIStandby, bool_t & EDROOMpVarVTimerActive, CEDROOMPOOLCDCommConfig & EDROOMpPoolCDCommConfig ):

	EDROOMcomponent(act)
	,Msg(EDROOMcomponent.Msg)
	,MsgBack(EDROOMcomponent.MsgBack)
	,EmuPlannerSGIControl(EDROOMcomponent.EmuPlannerSGIControl)
	,SSICtrl(EDROOMcomponent.SSICtrl)
	,CommuteTimer(EDROOMcomponent.CommuteTimer)
	, VSGIStandby ( EDROOMpVarVSGIStandby )
	, VTimerActive ( EDROOMpVarVTimerActive )
	, EDROOMPoolCDCommConfig ( EDROOMpPoolCDCommConfig )
 {

}

CAEmuBoardPlanner::EDROOMTopContext::EDROOMTopContext ( EDROOMTopContext &contex ):

	EDROOMcomponent(contex.EDROOMcomponent),
	Msg(contex.Msg),
	MsgBack(contex.MsgBack)
	,EmuPlannerSGIControl(contex.EmuPlannerSGIControl)
	,SSICtrl(contex.SSICtrl)
	,CommuteTimer(contex.CommuteTimer)
	, VSGIStandby ( contex.VSGIStandby )
	, VTimerActive ( contex.VTimerActive )
	, EDROOMPoolCDCommConfig ( contex.EDROOMPoolCDCommConfig )
 {

}

	// EDROOMSearchContextTrans********************************************

bool CAEmuBoardPlanner::EDROOMTopContext::EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans){

	bool edroomValidMsg=false; 

	 switch(Msg->signal){

		 case ( EDROOMSignalDestroy ): 

			 edroomValidMsg=true;
			 edroomCurrentTrans.distanceToContext = 0 ;
			 edroomCurrentTrans.localId = -1 ;
			 break;

	}

	return(edroomValidMsg);

}

	// Funciones Definidas por el usuario  ****************************

void	CAEmuBoardPlanner::EDROOMTopContext::FCommInterrupted()

{
 
	CDCommConfig * ap_CDCommConfig = EDROOMPoolCDCommConfig.AllocData();
	
		// Complete Data 
	
//	ap_CDCommConfig
	
		// invoke 
	
	MsgBack=EmuPlannerSGIControl.invoke(SKeepOnComm, EDROOMprioVeryHigh, ap_CDCommConfig, &EDROOMPoolCDCommConfig);
	
		// Handling MsgBack
	
 
	
	
		// End Handling MsgBack
	
	EDROOMFreeMsgBack(MsgBack);
 
 
 
 
}



void	CAEmuBoardPlanner::EDROOMTopContext::FInit()

{
 
		// send new Msg
	
	SSICtrl.send(SToListening, EDROOMprioNormal);
 
 
 
 
}



void	CAEmuBoardPlanner::EDROOMTopContext::FSGICommunication()

 
{
 
 if (VSGIStandby)
{
	CDCommConfig * ap_CDCommConfig = EDROOMPoolCDCommConfig.AllocData();
	
		// Complete Data 
	
	//ap_CDCommConfig
	
		// invoke 
	
	MsgBack=EmuPlannerSGIControl.invoke(SToComm, EDROOMprioVeryHigh, ap_CDCommConfig, &EDROOMPoolCDCommConfig);
	
		// Handling MsgBack
	
	
	VSGIStandby=false;
	
		// End Handling MsgBack
	
	EDROOMFreeMsgBack(MsgBack);
 
if(!VTimerActive)
{
	VTimerActive=true;
	Pr_Time	interval(80.0); // Declare time var with an initial value in seconds
	 
	CommuteTimer.InformIn( interval, EDROOMprioVeryHigh );
 
}
 
}
 
 
 
}



void	CAEmuBoardPlanner::EDROOMTopContext::FSSICommunication()

 
 
{
	if(!VTimerActive)
	{
	Pr_Time	interval(40.0); // Declare time var with an initial value in seconds
	 
	CommuteTimer.InformIn( interval, EDROOMprioNormal );
	}
 
 
 
 
}



void	CAEmuBoardPlanner::EDROOMTopContext::FTMCompleted()

{
 
		// send new Msg
	
	EmuPlannerSGIControl.send(SToStandBy, EDROOMprioNormal);
 	VSGIStandby=true;
 
 
 
}



void	CAEmuBoardPlanner::EDROOMTopContext::FToSGIComm()

{
 	Pr_IRQManager::DisableIRQ(UART_IRQ_NUM);
	bufferOfDeviceIndex=SGI_BUFFER;
	Pr_IRQManager::EnableIRQ(UART_IRQ_NUM);

		// send new Msg
	
	SSICtrl.send(SToStandBy, EDROOMprioNormal);
 
 
 
 
}



void	CAEmuBoardPlanner::EDROOMTopContext::FToSSIComm()

{
	Pr_IRQManager::DisableIRQ(UART_IRQ_NUM);
	bufferOfDeviceIndex=SSI_BUFFER;
	Pr_IRQManager::EnableIRQ(UART_IRQ_NUM);

	//VTimerActive=false;
		// send new Msg
	
	SSICtrl.send(SToListening, EDROOMprioNormal);
 
 
 
 
 
}



	//*****Pools

	//CEDROOMPOOLCDCommConfig

CAEmuBoardPlanner::EDROOMTopContext::CEDROOMPOOLCDCommConfig::CEDROOMPOOLCDCommConfig(unsigned int elemCount, CDCommConfig* pMem, TEDROOMBool * pMemMarks):CEDROOMProtectedMemoryPool(elemCount, pMem, pMemMarks, sizeof (CDCommConfig)){;}

CDCommConfig *	CAEmuBoardPlanner::EDROOMTopContext::CEDROOMPOOLCDCommConfig::AllocData(){
	return(CDCommConfig*)CEDROOMProtectedMemoryPool::AllocData();
}



// ***********************************************************************

// class EDROOMTopState

// ***********************************************************************



	// CONSTRUCTOR*************************************************

CAEmuBoardPlanner::EDROOMTopState::EDROOMTopState (CAEmuBoardPlanner &act, CEDROOMMemory *pEDROOMMemory  ):
	  EDROOMTopContext( act , VSGIStandby, VTimerActive,EDROOMPoolCDCommConfig )
	, VSGIStandby ( true )
	, VTimerActive ( false )
	, EDROOMPoolCDCommConfig (SIZE_OF_POOLS, pEDROOMMemory->poolCDCommConfig, pEDROOMMemory->poolMarkCDCommConfig)
{

}

	// EDROOMBehaviour**********************************************

void CAEmuBoardPlanner::EDROOMTopState::EDROOMBehaviour(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans = EDROOMIllegada();

	do{

		switch(edroomCurrentTrans.localId){

			case(Init):
				FInit();
				edroomNextState = SSICommunication;
				break;
			case(CommToSGI):
				FToSGIComm();
				edroomNextState = SGICommunication;
				break;
			case(CommToSSI):
				FToSSIComm();
				edroomNextState = SSICommunication;
				break;
			case(CommInterrupted):
				FCommInterrupted();
				edroomNextState = SGICommunication;
				break;
			case(TMTxCompleted):
				FTMCompleted();
				edroomNextState = SGICommunication;
				break;
		}

		EDROOMcomponent.EDROOMTraceStateEntry();

		EDROOMcomponent.EDROOMTraceStateEntry();

		switch(edroomNextState){

			case(I):
				edroomCurrentTrans=EDROOMIllegada();
				break;

			case(SSICommunication):
				FSSICommunication();
				edroomCurrentTrans=EDROOMSSICommunicationllegada();
				break;

			case(SGICommunication):
				FSGICommunication();
				edroomCurrentTrans=EDROOMSGICommunicationllegada();
				break;

		}

		EDROOMcomponent.EDROOMTraceStateExit();

		edroomCurrentState=edroomNextState;

	}while(Msg->signal != EDROOMSignalDestroy);

}



	// Inicio**********************************************

void CAEmuBoardPlanner::EDROOMTopState::EDROOMInicio(){

edroomCurrentState=I;

}



//	 ***********************************************************************

//	 llegada SubEstado I

//	 ***********************************************************************



TEDROOMTransId CAEmuBoardPlanner::EDROOMTopState::EDROOMIllegada(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans.localId = Init;
	edroomCurrentTrans.distanceToContext = 0;
	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, 0);

	return(edroomCurrentTrans);

}



	// ***********************************************************************

	// llegada SubEstado Hoja SSICommunication

	// ***********************************************************************



TEDROOMTransId CAEmuBoardPlanner::EDROOMTopState::EDROOMSSICommunicationllegada(){

	TEDROOMTransId edroomCurrentTrans;

	int edroomValidMsg=false;

	do{

		EDROOMNuevoMens ();

		switch(Msg->signal){

			case (EDROOMSignalTimeout): 

				 if ( *Msg->GetPInterface() == CommuteTimer) {

					edroomCurrentTrans.localId= CommToSGI;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

		};

		if (!edroomValidMsg) edroomValidMsg = EDROOMSearchContextTrans(edroomCurrentTrans);

	} while (!edroomValidMsg);

	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, Msg->signal);

	return(edroomCurrentTrans);

}



	// ***********************************************************************

	// llegada SubEstado Hoja SGICommunication

	// ***********************************************************************



TEDROOMTransId CAEmuBoardPlanner::EDROOMTopState::EDROOMSGICommunicationllegada(){

	TEDROOMTransId edroomCurrentTrans;

	int edroomValidMsg=false;

	do{

		EDROOMNuevoMens ();

		switch(Msg->signal){

			case (EDROOMSignalTimeout): 

				 if ( *Msg->GetPInterface() == CommuteTimer) {

					edroomCurrentTrans.localId= CommToSSI;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

			case (SCommInterrupted): 

				 if ( *Msg->GetPInterface() == EmuPlannerSGIControl) {

					edroomCurrentTrans.localId= CommInterrupted;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

			case (STMTransmited): 

				 if ( *Msg->GetPInterface() == EmuPlannerSGIControl) {

					edroomCurrentTrans.localId= TMTxCompleted;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

		};

		if (!edroomValidMsg) edroomValidMsg = EDROOMSearchContextTrans(edroomCurrentTrans);

	} while (!edroomValidMsg);

	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, Msg->signal);

	return(edroomCurrentTrans);

}



